package com.example.demo.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.demo.common.Result;
import com.example.demo.entity.ExInfo;
import com.example.demo.entity.ExLetters;
import com.example.demo.entity.SysUser;
import com.example.demo.exception.CustomException;
import com.example.demo.mapper.ExInfoMapper;
import com.example.demo.mapper.ExLettersMapper;
import com.example.demo.service.ExInfoService;
import com.example.demo.service.ExLettersService;
import com.example.demo.utils.TokenUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.constraints.NotBlank;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Optional;

@RestController
@RequestMapping("/exLetter")
public class ExletterController extends BaseController{

    @Resource
    ExLettersMapper exLettersMapper;

    @Autowired
    ExLettersService exLettersService;

    @PostMapping
    public Result<?> save(@RequestBody ExLetters exLetters, HttpServletRequest request) {
        exLetters.setCreateTime(new Date());
        exLettersMapper.insert(exLetters);
        return Result.success();
    }

    @PutMapping
    public Result<?> update(@RequestBody ExLetters exLetters,HttpServletRequest request) {
        SysUser user = TokenUtils.getUserInfo(request);
        if (user == null)
            return Result.error("500", "未获取到user信息");
        exLetters.setUpdateUser(user.getUsername());
        exLetters.setUpdateTime(new Date());
        exLettersMapper.updateById(exLetters);
        return Result.success();
    }

    @Transactional
    @DeleteMapping("/{ids}")
    public Result<?> delete(@PathVariable @NotBlank String ids, HttpServletRequest request) {
        SysUser user = TokenUtils.getUserInfo(request);
        if (user == null)
            return Result.error("500", "未获取到user信息");
        List<String> idList = Arrays.asList(ids.split(","));
        for (String ID : idList) {
            ExLetters exLetters = exLettersMapper.selectById(ID);
            Optional.ofNullable(exLetters).orElseThrow(() ->  new CustomException("500001","该数据不存在"));
            exLetters.setUpdateUser(user.getUsername());
            exLetters.setUpdateTime(new Date());
            exLettersMapper.updateById(exLetters);
            exLettersMapper.deleteById(exLetters.getId());
        }
        return Result.success("删除成功");
    }

//    @DeleteMapping("/{id}")
//    public Result<?> delete(@PathVariable Integer id) {
//        financeMapper.deleteById(id);
//        return Result.success();
//    }


    @GetMapping("/{id}")
    public Result<?> getById(@PathVariable Integer id) {
        return Result.success(exLettersMapper.selectById(id));
    }

/**
 * 
 * @author fqc
 * @date 2023/12/7 16:19
 * @param pageNum 页码
 * @param pageSize 数量
 * @param project 项目名称
 * @param merchant 商户名称
 * @param startTime 开始时间
 * @param endTime  结束时间
 * @return com.example.demo.common.Result<?>
 */
    @GetMapping
    public Result<?> findPage(@RequestParam(defaultValue = "1") Integer pageNum,
                              @RequestParam(defaultValue = "10") Integer pageSize,
                              @RequestParam(defaultValue = "") String project,
                              @RequestParam(defaultValue = "") String merchant,
                              @RequestParam(defaultValue = "") String startTime,
                              @RequestParam(defaultValue = "") String endTime) {
        LambdaQueryWrapper<ExLetters> wrapper = Wrappers.<ExLetters>lambdaQuery();
//        if (StrUtil.isNotBlank(project)) {
//            wrapper.like(ExInfo::getProject, project);
//        }
//        if (StrUtil.isNotBlank(merchant)) {
//            wrapper.like(ExInfo::getMerchant, merchant);
//        }
//        if(StrUtil.isNotBlank(startTime )|| StrUtil.isNotBlank(endTime)){
//            wrapper.and(q -> {
//                if(StrUtil.isNotBlank(startTime)) {
//                    q.ge(ExInfo::getPayTime, startTime);
//                }
//                if(StrUtil.isNotBlank(endTime)){
//                    q.le(ExInfo::getPayTime,endTime);
//                }
//            });
//        }
        Page<ExLetters> financePage = exLettersMapper.selectPage(new Page<>(pageNum, pageSize), wrapper);
        return Result.success(financePage);
    }

    @GetMapping("/list")
    public Result<?> list(){
        List<ExLetters> timetables = exLettersMapper.selectList(null);
        return Result.success(timetables);
    }

}
